import CommonLoopEvent from "../../event/eventdef/CommonLoopEvent";
import { PlayerRewardUIInfo, RewardUIDtl } from "../../types/L_Type"



/**  */
export default class RewardUICreator {
    
    static beCrateUIInfoMap:Map<number,PlayerRewardUIInfo> = new Map<number,PlayerRewardUIInfo>();
 
    public static createPlayerRewardUI(playerIndex:number,rewardDtls:RewardUIDtl[]){
        
        if(!RewardUICreator.beCrateUIInfoMap.has(playerIndex)){
            let consumerEvent=new CommonLoopEvent('SECONDS',1,()=>{
                RewardUICreator.consumePlayerRewardSend(playerIndex)
            })
            consumerEvent.beAddedAndExec()
            RewardUICreator.beCrateUIInfoMap.set(playerIndex,{
                playerIndex:playerIndex, //奖励玩家索引1-N
                consumerEvent:consumerEvent,  //循环执行面板呼出的事件管理器  方便管理队列问题
                groupDtls:[rewardDtls],
                waitForPlayerSelect:true
              })
        }else{
            let playerRewardInfo=RewardUICreator.beCrateUIInfoMap.get(playerIndex)
            if(playerRewardInfo==undefined){
                return
            }
            playerRewardInfo.groupDtls.push(rewardDtls)
            if(!playerRewardInfo.waitForPlayerSelect){
                playerRewardInfo.waitForPlayerSelect=true
                playerRewardInfo!.consumerEvent.resumePause()
            }

        }
        //以队列的形式进行消费 防止重叠冲突的发生 因为用户有选择等待时间 
        // if(RewardUICreator.beCrateUIInfoMap.get(playerRewardUIInfo.playerIndex)?.length==1){
        //     RewardUICreator.consumePlayerRewardSend(playerRewardUIInfo.playerIndex)
        // }
        
    }
 


    private static consumePlayerRewardSend(playerIndex:number){
        let playerRewardInfo=RewardUICreator.beCrateUIInfoMap.get(playerIndex)
        //事件暂停 等待用户选择后重启
        playerRewardInfo?.consumerEvent.pauseEvent()
        
        let rewardInfos=playerRewardInfo?.groupDtls?.shift()   //删除第一个元素，并返回该元素的值  （消费）
        if(playerRewardInfo==undefined || rewardInfos==undefined){
            return
        }else{
            console.log("剩余执行战利品发放次数>>>>>>>>>>>>>>",rewardInfos.length)
        }
        if(GetPlayerId(GetLocalPlayer())!=playerIndex){
            return
        }
        let gameUI=DzGetGameUI()
        DzLoadToc("ui\\custom.toc")
        let rewardFrameArr=[] as any[]
        for(let k=0;k<rewardInfos.length;k++){
            //循环创建每个奖励的UI框体
            let rewardInfo=rewardInfos[k]
            //三个框的x轴偏移量
            let xFix=-0.24
            if(k==1){
                xFix=0
            }else if(k==2){
                xFix=0.24
            }
    
            //创建每个奖励框的背景和主体UI框
            //let backgroundIframe=DzCreateFrame("TextBackDrop",gameUI,0)
            let backgroundIframe=DzCreateFrameByTagName("BACKDROP","",gameUI,"TextBackDrop",0)
            rewardFrameArr.push(backgroundIframe)
            DzFrameSetSize( backgroundIframe, 0.15, 0.25 )
            DzFrameShow( backgroundIframe, true )
            //DzFrameSetPoint( backgroundIframe, 4, DzGetGameUI(), 4, -0.24, -0.05 )  
            DzFrameSetPoint( backgroundIframe, 4, DzGetGameUI(), 4, xFix, 0 )  

            //let imgiframe = DzCreateFrame("Mbackdorp", backgroundIframe, 0)
            let imgiframe = DzCreateFrameByTagName("BACKDROP","", backgroundIframe,"Mbackdorp", 0)
            rewardFrameArr.push(imgiframe)
             DzFrameSetSize( imgiframe, 0.05, 0.05 )
             DzFrameSetTexture( imgiframe, rewardInfo.iconPath?rewardInfo.iconPath:"ReplaceableTextures\\CommandButtons\\BTNSelectHeroOn.blp", 0 )
             DzFrameShow( imgiframe, true )
             DzFrameSetPoint( imgiframe, 1, backgroundIframe, 1, 0, -0.02 )

             //奖励品质
             let  rareTextFrame = DzCreateFrameByTagName("TEXT","", backgroundIframe,"CustomText", 0)
             rewardFrameArr.push(rareTextFrame)
             DzFrameSetText( rareTextFrame, rewardInfo.rareDesc )
             DzFrameSetSize( rareTextFrame, 0.05, 0.05 )
             DzFrameShow( rareTextFrame, true )
             //DzFrameSetTextAlignment(titleTextFrame,1)
             DzFrameSetPoint( rareTextFrame, 6, backgroundIframe, 6,0, 0.04 ) 


            //let  titleTextFrame = DzCreateFrame("CustomText", backgroundIframe, 0)
            let  titleTextFrame = DzCreateFrameByTagName("TEXT","", backgroundIframe,"CustomText", 0)
            rewardFrameArr.push(titleTextFrame)
            DzFrameSetText( titleTextFrame, rewardInfo.rewardTtile )
            DzFrameSetSize( titleTextFrame, 0.05, 0.05 )
            DzFrameShow( titleTextFrame, true )
            //DzFrameSetTextAlignment(titleTextFrame,1)
            DzFrameSetPoint( titleTextFrame, 6, backgroundIframe, 6,0, 0.07 ) 
           //DzFrameSetPoint( titleTextFrame, 4, backgroundIframe, 4,0, 0.01 ) 
        
            // let  descTextFrame = DzCreateFrame("CustomText", backgroundIframe, 0)
            let  descTextFrame = DzCreateFrameByTagName("TEXT","", backgroundIframe,"", 0)
            rewardFrameArr.push(descTextFrame)
            DzFrameSetText( descTextFrame, rewardInfo.rewardDesc )
            DzFrameSetSize( descTextFrame, 0.1, 0.1 )
            DzFrameShow( descTextFrame, true )
            //DzFrameSetTextAlignment(descTextFrame,2)
            DzFrameSetPoint( descTextFrame, 4, backgroundIframe, 4,0, -0.04 ) 

            //每个奖励框的确认按钮
            //let button1=DzCreateFrame("TestButton",backgroundIframe,0)
            let button1=DzCreateFrameByTagName("GLUETEXTBUTTON","",backgroundIframe,"TestButton",0)
            rewardFrameArr.push(button1)
            DzFrameSetPoint(button1,4,backgroundIframe,4,0,-0.085)
            //DzFrameSetText(DzFrameFindByName("ButtonText",0),"选择")
            DzFrameSetText(button1,"选择")
            DzFrameSetScriptByCode(button1,1,
                function(){
                    console.log("===============DDDDDDDDDDDDDDDDD")
                     //尝试继续分发玩家奖励  可能存在队列其他值
                    //RewardUICreator.consumePlayerRewardSend(rPlayerIndex)
                   if(playerRewardInfo!.groupDtls.length>0){
                    playerRewardInfo!.consumerEvent.resumePause()
                   }else{
                    playerRewardInfo!.waitForPlayerSelect=false; //当前已选择完成可以执行下一次
                   }
                   

                    rewardInfo.callback() 
                    // 任选其一 删除框体 从后往前删除UI框体 还需要考虑触发玩家问题?
                    for(let i=rewardFrameArr.length-1;i>=0;i--){
                        DzDestroyFrame(rewardFrameArr[i])
                    }
                   
                   
                    
                }
                ,true)

        }


    }

  
}  